package cn.ac.jaas.isc.vegetable.quartz.util;

import cn.ac.jaas.isc.vegetable.common.core.constant.CommonConstants;
import cn.ac.jaas.isc.vegetable.quartz.entity.SysJob;
import cn.ac.jaas.isc.vegetable.quartz.exception.TaskException;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * @author 张高昌
 * @date 2022/4/30 16:48
 * @description: 定时任务rest反射实现
 */
@Slf4j
@AllArgsConstructor
@Component("restTaskInvoke")
public class RestTaskInvoke implements ITaskInvoke {

    @Override
    public void invokeMethod(SysJob sysJob) throws TaskException {
        try {
            HttpRequest request = HttpUtil.createGet(sysJob.getExecutePath());
            if (sysJob.getTenantId() != null) {
                request.header(CommonConstants.TENANT_ID, sysJob.getTenantId().toString());
            }
            request.execute();
        } catch (Exception e) {
            log.error("定时任务restTaskInvok异常,执行任务：{}", sysJob.getExecutePath());
            throw new TaskException("定时任务restTaskInvok业务执行失败,任务：" + sysJob.getExecutePath());
        }
    }
}
